package cn.timelost.hr.service.impl;

import cn.timelost.hr.dao.AttendanceDao;
import cn.timelost.hr.pojo.Attendance;
import cn.timelost.hr.pojo.Attendance2;
import cn.timelost.hr.service.AttendanceService;
import cn.timelost.hr.vo.UnAttendanceVo;
import cn.timelost.hr.vo.input.AttendanceForm;

import cn.timelost.hr.config.utils.SysTime;
import cn.timelost.hr.enums.ResultEnum;
import cn.timelost.hr.exception.BaseException;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "attendance")
public class AttendanceServiceImpl implements AttendanceService {

    @Resource
    private AttendanceDao attendanceDao;
    int rrr=1;


    @Override
    public PageInfo<UnAttendanceVo> findUnAttendanceAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
//        java.sql.Date date2=new java.sql.Date(searchDate.getTime());
        List<UnAttendanceVo> unAttendanceVos = attendanceDao.getUnAttendance();
        return new PageInfo<>(unAttendanceVos);
    }

    @Override
    @Cacheable(key = "#personalId+'-'+#beginDate+'-'+#endDate+'-'+#pageNum+'-'+#pageSize")
    public PageInfo<Attendance2> findAllView(int pageNum, int pageSize, int personalId, Date beginDate, Date endDate) {
//        Attendance attendance222=new Attendance();
//
//        SysTime sysTime=new SysTime();
//        attendance222.setId(0);
//        attendance222.setPersonId(0);
//        attendance222.setDate(sysTime.getDate());
//        attendance222.setTime(sysTime.getTime());
//        attendance222.setStatus(9);
//        System.out.println("myhe"+attendance222);
//        attendanceDao.insert(attendance222);
//        attendanceDao.deleteByPID();

        if(rrr==0){
            rrr=1;
            attendanceDao.updatefake();
        }else{
            rrr=0;
            attendanceDao.updatefake2();
        }

        if (ObjectUtils.isEmpty(beginDate)) {
            beginDate = null;
        }
        PageHelper.startPage(pageNum, pageSize);
//        java.sql.Date date2=new java.sql.Date(searchDate.getTime());
        List<Attendance2> attendances = attendanceDao.selectView(personalId,beginDate, endDate);
        return new PageInfo<>(attendances);
    }

    @Override
    @Cacheable(key = "#personalId+'-'+#beginDate+'-'+#endDate+'-'+#pageNum+'-'+#pageSize")
    public PageInfo<Attendance> findAll(int pageNum, int pageSize, int personalId, Date beginDate, Date endDate) {
        if (ObjectUtils.isEmpty(beginDate)) {
            beginDate = null;
        }
        PageHelper.startPage(pageNum, pageSize);
//        java.sql.Date date2=new java.sql.Date(searchDate.getTime());
        List<Attendance> attendances = attendanceDao.selectAll(personalId,beginDate, endDate);
        return new PageInfo<>(attendances);
    }

    @Override
    @Cacheable(key = "#root.methodName")
    public List<Attendance> all() {
        return attendanceDao.selectAll(0, null,null);
    }



    @Override
    @Cacheable(key = "#root.methodName")
    public List<Attendance2> allView() {
        return attendanceDao.selectView(0, null,null);
    }

    @Override
    public Attendance find(int id) {
        Attendance attendance = attendanceDao.selectByPrimaryKey((long) id);
        if (ObjectUtils.isEmpty(attendance)) {
            throw new BaseException(ResultEnum.PERSONAL_ATTENDANCE_NOT_EXIST);
        }
        return attendance;
    }

    @Override
    public Attendance signUp(AttendanceForm attendanceForm) {
        int personalId=attendanceForm.getPersonalId();
        Attendance attendance=new Attendance();
        attendance.setPersonId(personalId);
        SysTime sysTime=new SysTime();
        attendance.setDate(sysTime.getDate());
        attendance.setTime(sysTime.getTime());
        attendance.setStatus(sysTime.judge());
        return attendance;
    }

    /**
     * 签到
     * @param
     * @return
     */
    @Override
    public Attendance signUp(int personalId){
        Attendance attendance=new Attendance();

        SysTime sysTime=new SysTime();
        attendance.setId(0);
        attendance.setPersonId(personalId);
        attendance.setDate(sysTime.getDate());
        attendance.setTime(sysTime.getTime());
        attendance.setStatus(sysTime.judge());
        System.out.println("myhe"+attendance);
        attendanceDao.insert(attendance);
        return attendance;
    }

    @Override
    public Attendance signUpUn(int personalId) {
        Attendance attendance=new Attendance();

        SysTime sysTime=new SysTime();
        attendance.setId(0);
        attendance.setPersonId(personalId);
        attendance.setDate(sysTime.getDate());
        attendance.setTime(sysTime.getTime());
        attendance.setStatus(0);
        System.out.println("myhe"+attendance);
        attendanceDao.insert(attendance);
        return attendance;
    }
}
